# -*- coding:utf-8 -*-
from flask.ext import restful
from flask.ext.restful import fields, marshal_with, reqparse,Resource,marshal_with
import sys
from flask.ext.restful import reqparse
import MySQLdb
from objectClass.mysqlOjbect import MysqlGroup
import json

reload(sys)
sys.setdefaultencoding('utf-8')
#print sys.getdefaultencoding()
conn = MySQLdb.connect(host='192.168.1.104',port=3306,user='root',db='zeus',passwd='123465')
#conn = MySQLdb.connect(host='10.20.0.254',port=3306,user='root',db='zeus',passwd='123465')

cursor = conn.cursor()

getMysqlGroupFields = {
    'id': fields.Integer,
    'group_name': fields.String,
    'sort_id': fields.Integer,
}

class getMysqlGroups(restful.Resource):

    def getMysqlGroups(self):
        getMysqlIdSql="select id,group_name,sort_id from mysql_group order by sort_id"
        print getMysqlIdSql
        cursor.execute(getMysqlIdSql)
        groupResult = cursor.fetchall()
        groupList = []
        for group in groupResult:
            groupList.append(MysqlGroup(group[0],group[1],group[2]))
            #print (group[1]).decode('utf-8')
            print group[1]
        return groupList


    @marshal_with(getMysqlGroupFields)
    def get(self, **kwargs):
        data = self.getMysqlGroups()
        if data:
            print type(data)
            return data,200
        else:
            restful.abort(404)





